package com.example.myapplication;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ResultFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ResultFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String RESULTS = "RESULTS";

    // TODO: Rename and change types of parameters
    private ArrayList<Integer> results;
    private ArrayList<TextView> textViews;
    private TextView correct_counter;

    public ResultFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param results Parameter 1.
     * @return A new instance of fragment ResultFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ResultFragment newInstance(ArrayList<Integer> results) {
        ResultFragment fragment = new ResultFragment();
        Bundle args = new Bundle();
        args.putIntegerArrayList(RESULTS, results);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            results = getArguments().getIntegerArrayList(RESULTS);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_result, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textViews = new ArrayList<>();
        textViews.add(view.findViewById(R.id.star1));
        textViews.add(view.findViewById(R.id.star2));
        textViews.add(view.findViewById(R.id.star3));
        textViews.add(view.findViewById(R.id.star4));

        correct_counter = view.findViewById(R.id.correct_counter);
        int count = 0;
        for (int i = 0; i < 4; i ++)
        {
            count += results.get(i);
        }
        correct_counter.setText(""+count);


        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_show_result);
        for (int i = 0; i < 4; i ++)
        {
            if (results.get(i) > 0)
            {
                textViews.get(i).setBackground(getResources().getDrawable(R.drawable.full_star));
                textViews.get(i).startAnimation(animation);
            }
        }
    }
}